/**
 * 307. 区域和检索 - 数组可修改
 * https://leetcode-cn.com/problems/range-sum-query-mutable/
 * 进阶：树状数组、线段树
 */
public class Solutions_307 {
    public static void main(String[] args) {
        int[] nums = {1, 3, 5};
        NumArray obj = new NumArray(nums);
        int res1 = obj.sumRange(0, 2);  // output: 9
        System.out.println(res1);
        obj.update(1, 2);
        int res2 = obj.sumRange(0, 2);  // output: 8
        System.out.println(res2);
    }
}

/**
 * 解法一：暴力法（68ms）
 */
class NumArray {
    int[] nums = null;

    public NumArray(int[] nums) {
        this.nums = nums;
    }

    public void update(int i, int val) {
        if (i >= 0 && i < nums.length) {
            nums[i] = val;
        }
    }

    public int sumRange(int i, int j) {
        int res = 0;
        while (i <= j) {
            res += nums[i];
            i++;
        }
        return res;
    }
}
